package org.happen.android.widget;

import org.liushui.mycommons.android.annotation.ViewInject;
import org.liushui.mycommons.android.annotation.helper.InjectHelper;
import org.liushui.mycommons.android.util.McViewUtil;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import org.happen.android.R;

public class NormalProgressDialog
{
	Context context;
	Dialog dialog;

	View contentView;
	@ViewInject(R.id.tv_msg)
	TextView tvMsg;
	@ViewInject(R.id.iv_loading)
	ImageView ivLoading;

	String titleText, messageText;
	String okText, cancelText;

	public NormalProgressDialog(Context context)
	{
		this.context = context;
	}

	void init()
	{
		contentView = View.inflate(context, R.layout.dialog_progress, null);
		InjectHelper.init(this, contentView);
	}

	public Dialog create()
	{
		init();

		dialog = new Dialog(context, R.style.AppTheme_Dialog_NoTitleBar);
		dialog.setContentView(contentView);

		setMessage(null);

		return dialog;
	}

	public void show()
	{
		if (dialog == null)
		{
			return;
		}
		dialog.show();
		contentView.postDelayed(new Runnable() {
			public void run()
			{
				AnimationDrawable animationDrawable = (AnimationDrawable) ivLoading.getBackground();
				animationDrawable.start();
			}
		}, 200);
	}

	public void dismiss()
	{
		if (dialog != null)
		{
			dialog.dismiss();
		}
	}

	public void setMessage(int msgId)
	{
		setMessage(contentView.getResources().getString(msgId));
	}

	public void setMessage(String msg)
	{
		tvMsg.setText(msg);
		McViewUtil.showORHiden(tvMsg, !TextUtils.isEmpty(msg));
	}

	public Dialog getDialog()
	{
		return dialog;
	}
}